package com.shgsh.platform.service;

import com.alibaba.fastjson.JSON;
import com.shgsh.attribute.model.WeatherData;
import com.shgsh.common.repository.CURDRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * Created by Evelyn on 2015/8/8.
 */
@Service
public class WeatherDataService {
    private static String mapName = "emotion_weather_data";
    @Autowired
    CURDRepository curdRepository;

    public void add(WeatherData weatherData) {
        if (weatherData.getWeather_id().isEmpty()) {
            String uuid = UUID.randomUUID().toString();
            weatherData.setWeather_id(uuid);
        }
        ArrayList weatherDataParam = new ArrayList();
        weatherDataParam.add(mapName.getBytes());
        weatherDataParam.add(weatherData.getWeather_id().getBytes());
        weatherDataParam.add(weatherData.toJson().getBytes());
        curdRepository.hset(weatherDataParam);
    }

    public WeatherData get(String id) {
        ArrayList<byte[]> param = new ArrayList<>();
        param.add(mapName.getBytes());
        param.add(id.getBytes());
        String weatherString = curdRepository.hget(param);
        WeatherData weatherData = JSON.parseObject(weatherString, WeatherData.class);
        return weatherData;
    }

    public ArrayList getAll() {
        Map weatherDataMap = curdRepository.hscanAll(mapName);
        Set<Map.Entry> jsonSet = weatherDataMap.entrySet();
        ArrayList list = new ArrayList();
        for (Map.Entry<String, String> data : jsonSet) {
            System.out.println(data);
            WeatherData weather = JSON.parseObject(data.getValue(), WeatherData.class);
            list.add(weather);
        }
        return list;
    }

    public void delete(String id) {
        ArrayList<byte[]> param = new ArrayList<>();
        param.add(mapName.getBytes());
        param.add(id.getBytes());
        curdRepository.hdel(param);
    }


}
